<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      :title="'文章详情-'+ article.title"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading
      v-if="loading"
      class="article-loading"
    />
    <!-- /加载中 loading -->
    <div
      class="error"
      v-if="is404"
    >
      <p>文章没了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <author :author="article">
        <!-- <van-image
          :src="article.aut_photo"
          round
          width="1rem"
          height="1rem"
          fit="fill"
        />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | time}}</p>
        </div> -->
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{article.is_followed ? '取关' : '+ 关注'}}</van-button>
      </author>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1?'good-job':'good-job-o'"
          @click="hSwitchLike"
        >{{article.attitude === 1? '取消点赞':'点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="hNOlove"
        >{{article.attitude === 0? '不太喜欢':'不喜欢'}}</van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 评论 -->
    <comment :articleId="$route.params.id"></comment>
  </div>
</template>

<script>
import { getDetail, delLike, addLike, notNolike, noLike } from '@/api/article.js'
import { followed, unFollowed } from '@/api/user.js'
import Comment from './comment.vue'
import Author from '@/components/author.vue'
export default {
  name: 'theArticle',
  data () {
    return {
      is404: false,
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  methods: {
    // 处理404
    // 对文章不喜欢和取消不喜欢
    async hNOlove () {
      try {
        const artId = this.article.art_id
        if (this.article.attitude === 0) {
          const art = await notNolike(artId)
          console.log(art)
          this.$toast.success('取消成功')
          this.article.attitude = -1
        } else {
          const art = await noLike(artId)
          console.log(art)
          this.$toast.success('不太喜欢')
          this.article.attitude = 0
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 点赞和取消点赞
    async hSwitchLike () {
      try {
        const artId = this.article.art_id
        if (this.article.attitude === 1) {
          const res = await delLike(artId)
          console.log(res)
          this.$toast.success('取赞成功')
          this.article.attitude = -1
        } else {
          const res = await addLike(artId)
          console.log(res)
          this.$toast.success('点赞成功')
          this.article.attitude = 1
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 关注和取关
    async hSwitchFollow () {
      try {
        const autId = this.article.aut_id
        const isFollowed = this.article.is_followed
        if (isFollowed) {
          const res = await unFollowed(autId)
          console.log(res)
          this.$toast.success('取关成功')
          // 修改页面的状态
          this.article.is_followed = false
        } else {
          const res = await followed(autId)
          console.log(res)
          this.$toast.success('关住成功')
          this.article.is_followed = true
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 获取文章详情
    async loadDetail () {
      try {
        this.is404 = false
        this.loading = true
        const res = await getDetail(this.$route.params.id)
        console.log(res)
        this.article = res.data.data
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        if (err.response.status === 404) {
          this.is404 = true
        }
      }
    }
  },
  created () {
    this.loadDetail()
  },
  components: {
    Comment, Author
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
